﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JinJun.YShowReception.Core
{
  public  class ConfigurationHelper  //配置文件帮助类
    {
        /// <summary>
        ///获取AppSettings节点下指定key的value值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetAppSettingsValue(string key)
        {
            //获取配置文件AppSetting下key的值
            var result = ConfigurationManager.AppSettings[key];
            //判断值是否为空
            if (result==null)
            {
                return "";
            }
            return result;
        }


        /// <summary>
        ///获取Section节点下指定key的value值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetSectionValue(string sectionName,string key)
        {
            //获取指定Section下的值
            IDictionary section =(IDictionary)ConfigurationManager.GetSection(sectionName);
            var result = section[key];
            //判断值是否为空
            if (result == null)
            {
                return "";
            }
            return result.ToString();
        }


        /// <summary>
        ///获取Section节点下key的集合
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static List<string> GetSectionKeyList(string sectionName)
        {
            //创建List对象
            List<string> resultList = new List<string>();
            //获取指定Section下的值
            IDictionary section = (IDictionary)ConfigurationManager.GetSection(sectionName);
            //循环遍历Section下的Key
            foreach (string item in section.Keys)
            {
                resultList.Add(item);
            }                    
            return resultList;
        }
    }
}
